package com.weiyuwqj.admin.controller.admin.asset;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.ItemStatus;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasAssetsPackage;
import com.weiyuwqj.api.model.BasItem;
import com.weiyuwqj.api.model.BusAccount;
import com.weiyuwqj.api.model.BusAssetInvest;
import com.weiyuwqj.api.model.BusAssetPackageInvest;
import com.weiyuwqj.api.model.BusAssetPackagePlan;
import com.weiyuwqj.api.model.vo.BusAssetPackagePlanVO;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2017/3/28.
 * 资产打包
 */
@Controller
@RequestMapping("/admin/asset")
public class BasAssetsPackageController extends BaseController {

    @Autowired
    private BasAssetsPackageService assetsPackageService;
    @Autowired
    private BasItemService itemService;
    @Autowired
    private NoTransationAssetPackagePlanService noTransationAssetPackagePlanService;
    @Autowired
    private BusAssetPackagePlanService assetPackagePlanService;
    @Autowired
    private BusAssetInvestService assetInvestService;
    @Autowired
    private BusAssetPackageInvestService packageInvestService;
    @Autowired
    private BusAccountService accountService;

    /**
     * 查询资产包列表
     *
     * @param p             分页参数
     * @param assetsPackage 包对象
     * @return
     */
    @ResponseBody
    @RequestMapping("/getPackageList")
    public Object getPackageList(Paginator p, BasAssetsPackage assetsPackage) {
        return assetsPackageService.getPackageList(p, assetsPackage);
    }

    /**
     * 资产打包
     *
     * @param packageName 包名称
     * @param itemStr     项目id字符串，以，隔开
     * @return
     */
    @ResponseBody
    @RequestMapping("/addAssetPackage")
    public Object addAssetPackage(String packageName, String itemStr) {
        String addip = IpUtils.get();
        Integer sysUserId = Integer.parseInt(this.getSession().getAttribute("userId").toString());
        assetsPackageService.insertPackage(itemStr, sysUserId, packageName, addip);
        return Message.OK;
    }

    /**
     * 通过包ID查询包下面的标
     *
     * @param packageId 包Id
     *                  暂时不分页
     */
    @ResponseBody
    @RequestMapping(value = "/getItemListByPackageId")
    public PageList getItemListByPackageId(Paginator p, Integer packageId) {
        return itemService.getItemListByPackageId(p, packageId);
    }

//    /**
//     * 包初审
//     *
//     * @param eachAmount 每份金额(包表)
//     * @param content    内容(包表)
//     * @param packageId  包ID
//     */
//    @ResponseBody
//    @RequestMapping(value = "/packageFirstTrial")
//    public Object packageFirstTrial(BigDecimal eachAmount, String content, Integer packageId) {
//        return itemService.packageFirstTrial(eachAmount, content, packageId);
//    }

//    /**
//     * 包复审
//     *
//     * @param assetsPackagePlanId 包计划id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/packageFullCheck")
//    public Object packageFullCheck(Integer assetsPackagePlanId, Integer fullVerifyStatus) {
//
//        BusAssetPackagePlan assetPackagePlan = assetPackagePlanService.selectByKey(assetsPackagePlanId);
//        //根据包id查询表列表
//        List<BasItem> itemList = itemService.getItemListByPackageId(assetPackagePlan.getAssetPackageId());
//        //判断复审是否通过
//        if (fullVerifyStatus == 0) {
//            //循环标列表，标作废（复审失败）
//            Boolean cancelBoolean = true;
//            for (BasItem item : itemList) {
//                try {
//                    assetsPackageService.cancelCallBack(item.getId());
//                } catch (Exception e) {
//                    itemService.updateItemStatus(15, 20, item.getId());
//                    cancelBoolean = false;
//                }
//            }
//            if (cancelBoolean) {
//                //修改包状态
//                assetsPackageService.modifyPackageStatusById(4, 2, assetPackagePlan.getAssetPackageId());
//            }
//        } else {
//            //还款时间
//            Date repayTime = null;
//            //循环标列表，标成立（复审成功）
//            for (BasItem item : itemList) {
//                //校验标是否已满标
//                if (item.getItemStatus() != ItemStatus.FULL_COMPLETE && item.getItemStatus() != ItemStatus.INTERCEPT_COMPLETE) {
//                    throw new CrowdException("item.status.not_full_complete");
//                }
//                //投资人动作
//                List<BusAssetInvest> assetInvestList = assetInvestService.getAssetInvestListByItemId(item.getId());
//                //解冻
//                if (item.getFullStatus() == 0) {
//                    assetsPackageService.assetThaw(assetInvestList, item.getId());
//                }
//                //批量投资
//                BasItem batchInvestItem = itemService.selectByKey(item.getId());
//                if (batchInvestItem.getFullStatus() == 1) {
//                    Map batchInvestMap = assetsPackageService.batchInvest(item, assetInvestList);
//                    if ("fail".equals(batchInvestMap.get("result"))) {
//                        return batchInvestMap;
//                    }
//                }
//                //成标
//                BasItem creatItem = itemService.selectByKey(item.getId());
//                if (creatItem.getFullStatus() == 2) {
//                    repayTime = assetsPackageService.assetCreatBid(item.getId(), repayTime, item, assetInvestList, assetPackagePlan.getAssetPackageId());
//                }
//
//            }
//            //更新包计划表
//            assetPackagePlanService.modifyRepayTimeById(repayTime, assetsPackagePlanId);
//            //更新包计划状态
//            assetPackagePlanService.modifyStatusById(ItemStatus.THE_MATCHED_AUDIT,assetsPackagePlanId,1);
//        }
//        return Message.OK;
//    }


//    /**
//     * 包还款
//     *
//     * @param packagePlanId 包计划id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/packageRepay")
//    public Object packageRepay(Integer packagePlanId) {
//        return noTransationAssetPackagePlanService.packageRepay(packagePlanId);
//    }
//
//    /**
//     * 借款人包计划还款
//     *
//     * @param packagePlanId 包计划id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/brrowerPackageRepay")
//    public Object brrowerPackageRepay(Integer packagePlanId) {
//        return noTransationAssetPackagePlanService.brrowerPackageRepay(packagePlanId);
//    }

//    /**
//     * 根据计划id修改状态
//     *
//     * @param packagePlanId
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/releasePlan")
//    public Object releasePlan(Integer packagePlanId){
//        assetPackagePlanService.modifyStatusById(ItemStatus.IN_THE_INVESTMENT,packagePlanId,2);
//        return Message.OK;
//    }

//    /**
//     * 扫包
//     *
//     * @param userId        用户ID
//     * @param packagePlanId 包计划ID
//     * @return 扫标结果
//     */
//    @ResponseBody
//    @RequestMapping(value = "/useEnterpriseRecharege", method = RequestMethod.POST)
//    public Object userEnterpriseRecharege(Integer userId, Integer packagePlanId) {
//        BusAccount account = accountService.getBusAccountByUserId(userId); // 账户查询
//        BusAssetPackagePlanVO assetPackagePlan = assetPackagePlanService.getAssetPackagePlanById(packagePlanId);// 扫描包下所有的标余额
//        BigDecimal subtract = assetPackagePlan.getAmount().subtract(assetPackagePlan.getOngoingAccount()); // 包剩余可投资金额
//        if (account.getUsable().compareTo(subtract) > 0) { // 账户可用余额大于标剩余可购买余额则进行投资getAssetPlanList
//            try {
//                return packageInvestService.doInvest(userId, assetPackagePlan.getNumber(), packagePlanId, 1); // 包投资
//            } catch (Exception e) {
//                e.printStackTrace();
//                Map<String, Object> resultMap = new HashMap<>();
//                resultMap.put("result", "fail");
//                resultMap.put("messageText", "包投资出错");
//                return resultMap;
//            }
//        } else {
//            Map<String, Object> resultMap = new HashMap<>();
//            resultMap.put("result", "fail");
//            resultMap.put("messageText", "账户可用余额不足");
//            return resultMap;
//        }
//
//    }

    /**
     * 根据项目id查询记录
     * @param p
     * @param itemId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInvestListByItemId", method = RequestMethod.POST)
    public Object getInvestListByItemId(Paginator p,Integer itemId){
        return assetInvestService.getInvestListByItemId(p, itemId);
    }

}
